#include "dataplot.h"
#include "stydatabase.h"
#include "main_left_widget.h"

dataPlot::dataPlot(QWidget *parent) : QWidget(parent)
{
    diagram = new QCustomPlot(this);
    setupDate(this->diagram);
}

void dataPlot::setupDate(QCustomPlot *customPlot)
{
  demoName = "学友进度综合统计";
  // set locale to english, so we get english month names:
  //customPlot->setLocale(QLocale(QLocale::English, QLocale::UnitedKingdom));
  // seconds of current time, we'll use it as starting point in time for data:
  double startDateTime = QDateTime(dateStart).toTime_t();
  // configure left axis text labels:
  QSharedPointer<QCPAxisTickerText> textTicker(new QCPAxisTickerText);
  for (int gi=0; gi<g_data.size(); ++gi)
  {
    QCPGraph *a = customPlot->addGraph();
    a->setName(g_data[gi].friends.UNickname);
    QColor color(20+200/4.0*gi,70*(1.6-gi/4.0), 150, 150);
    customPlot->graph()->setLineStyle(QCPGraph::lsLine);
    customPlot->graph()->setPen(QPen(color.lighter(200)));
    customPlot->graph()->setBrush(QBrush(color));
    // generate random walk data:
    QVector<QCPGraphData> timeData(31);
    for (int i=0; i<30; ++i)
    {
      timeData[i].key = startDateTime + 24*3600*i;
      timeData[i].value = g_data[gi].actual_Data[i];
    }
    textTicker->addTick(g_data[gi].friends.UTgDayTime*60,QString(g_data[gi].friends.UNickname+"的目标"));
    customPlot->graph()->data()->set(timeData);
  }
  // configure bottom axis to show date instead of number:
  QSharedPointer<QCPAxisTickerDateTime> dateTicker(new QCPAxisTickerDateTime);
  dateTicker->setDateTimeFormat("yyyy.MM.d");
  customPlot->xAxis->setTicker(dateTicker);
  customPlot->yAxis->setTicker(textTicker);
  // set a more compact font size for bottom and left axis tick labels:
  customPlot->xAxis->setTickLabelFont(QFont(QFont().family(), 8));
  customPlot->yAxis->setTickLabelFont(QFont(QFont().family(), 8));
  // set axis labels:
  customPlot->xAxis->setLabel("日期");
  customPlot->yAxis->setLabel("学习时长,单位/分钟");
  // make top and right axes visible but without ticks and labels:
  customPlot->xAxis2->setVisible(true);
  customPlot->yAxis2->setVisible(true);
  customPlot->xAxis2->setTicks(false);
  customPlot->yAxis2->setTicks(false);
  customPlot->xAxis2->setTickLabels(false);
  customPlot->yAxis2->setTickLabels(false);
  // set axis ranges to show all data:
  customPlot->xAxis->setRange(startDateTime, startDateTime+24*3600*30);
  customPlot->yAxis->setRange(0, 400);
  // show legend with slightly transparent background brush:
  customPlot->legend->setVisible(true);
  customPlot->legend->setBrush(QColor(255, 255, 255, 150));

  /* 设置背景色 */
  QLinearGradient gradient(0, 0, 0, 400);//黑色
  gradient.setColorAt(1, QColor(210, 210, 210));//灰色
  gradient.setColorAt(0, QColor(220, 220, 220));//淡一点的灰色
  gradient.setColorAt(0.38, QColor(230, 230, 230));//更淡的灰色
  customPlot->setBackground(QBrush(gradient));
}
